# --------------------------------------------------------------------------------
#  NoiseTube Web application
#  
#  Copyright (C) 2008-2010 SONY Computer Science Laboratory Paris
#  Portions contributed by Vrije Universiteit Brussel (BrusSense team), 2008-2011
# --------------------------------------------------------------------------------
#  This library is free software; you can redistribute it and/or modify it under
#  the terms of the GNU Lesser General Public License, version 2.1, as published
#  by the Free Software Foundation.
#  
#  This library is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
#  details.
#  
#  You should have received a copy of the GNU Lesser General Public License along
#  with this library; if not, write to:
#    Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor,
#    Boston, MA  02110-1301, USA.
#  
#  Full GNU LGPL v2.1 text: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#  NoiseTube project source code repository: http://code.google.com/p/noisetube
# --------------------------------------------------------------------------------
#  More information:
#   - NoiseTube project website: http://www.noisetube.net
#   - Sony Computer Science Laboratory Paris: http://csl.sony.fr
#   - VUB BrusSense team: http://www.brussense.be
# --------------------------------------------------------------------------------
 
class Cartasur::LocationsController < Cartasur::ApplicationController
  include Geokit::Geocoders

  def index
      @locations = Cartasur::Location.order(:name).all
  end

  def new
   @location = Cartasur::Location.new
   @location_types = Cartasur::List.getlist(Cartasur::List::TYPES[:places])
   @location.add_coordinates({lat: 34, lng: 4})
   @location_lat = @location.coordinates.x
   @location_lng = @location.coordinates.y
  end


  def show
    @location = Cartasur::Location.find(params[:id])
    @location_types = Cartasur::List.getlist(Cartasur::List::TYPES[:places])
  end

  def create
    location_hash = params[:cartasur_location]
    unless location_hash.nil?
         @location = Cartasur::Location.new(location_hash)

        @location.add_coordinates(params[:location_lat] << "," << params[:location_lng])

         if @location.save
           flash[:notice] = "Successfully saved! <a href='/cartasur/locations/new'>Add another</a>".html_safe
           redirect_to @location
         else
           flash[:alert] = "There was a problem when saving"
           render :action => 'new'
         end
    end

  end

  def edit
    @location  = Cartasur::Location.find(params[:id])
    @location_types = Cartasur::List.getlist(Cartasur::List::TYPES[:places])
    @location_lat = @location.coordinates.x
    @location_lng = @location.coordinates.y
  end
  def update
    @location = Cartasur::Location.find(params[:id])
    @location.add_coordinates(params[:location_lat] << "," << params[:location_lng])
    #store it
    mygeom = @location.coordinates
    if @location.update_attributes(params[:cartasur_location])
      flash[:notice] = "The item was successfully updated!"
        # temporary bug fix for update geom value
        @location.update_attribute(:coordinates, mygeom )
        redirect_to cartasur_locations_path
    else
      flash[:alert] = "There was a problem when updating"
    end
  end

end
